$(function () {
  $("body").on('click', '[data-stopPropagation]', function (e) {
    e.stopPropagation();
  });

  // 滚动条
  const ps = new PerfectScrollbar('.layout-sidebar-scroll', {
    swipeEasing: false,
    suppressScrollX: true
  });

  // 侧边栏
  $(".aside-toggler").bind('click', function () {
    $('.layout-sidebar').toggleClass('aside-open');
    $("body").toggleClass('layout-sidebar-close');

    if ($('.mask-modal').length == 0) {
      $('<div class="mask-modal"></div>').prependTo('body');
    } else {
      $('.mask-modal').remove();
    }
    $('.mask-modal').on('click', function () {
      $(this).remove();
      $('.layout-sidebar').toggleClass('aside-open');
      $('body').toggleClass('layout-sidebar-close');
    });
  });

  // 侧边栏导航
  $('.nav-item-has-subnav > a').on('click', function () {
    $subnavToggle = jQuery(this);
    $navHasSubnav = $subnavToggle.parent();
    $topHasSubNav = $subnavToggle.parents('.nav-item-has-subnav').last();
    $subnav = $navHasSubnav.find('.nav-subnav').first();
    $viSubHeight = $navHasSubnav.siblings().find('.nav-subnav:visible').outerHeight();
    $scrollBox = $('.layout-sidebar-scroll');
    $navHasSubnav.siblings().find('.nav-subnav:visible').slideUp(500).parent().removeClass('open');
    $subnav.slideToggle(300, function () {
      $navHasSubnav.toggleClass('open');

      // 新增滚动条处理
      var scrollHeight = 0;
      pervTotal = $topHasSubNav.prevAll().length,
        boxHeight = $scrollBox.outerHeight(),
        innerHeight = $('.sidebar-main').outerHeight(),
        thisScroll = $scrollBox.scrollTop(),
        thisSubHeight = $(this).outerHeight(),
        footHeight = 121;

      if (footHeight + innerHeight - boxHeight >= (pervTotal * 48)) {
        scrollHeight = pervTotal * 48;
      }
      if ($subnavToggle.parents('.nav-item-has-subnav').length == 1) {
        $scrollBox.animate({ scrollTop: scrollHeight }, 300);
      } else {
        // 子菜单操作
        if (typeof ($viSubHeight) != 'undefined' && $viSubHeight != null) {
          scrollHeight = thisScroll + thisSubHeight - $viSubHeight;
          $scrollBox.animate({ scrollTop: scrollHeight }, 300);
        } else {
          if ((thisScroll + boxHeight - $scrollBox[0].scrollHeight) == 0) {
            scrollHeight = thisScroll - thisSubHeight;
            $scrollBox.animate({ scrollTop: scrollHeight }, 300);
          }
        }
      }
    });
  });
  // 选项卡
  $('#iframe-content').multitabs({
    iframe: true,
    nav: {
      backgroundColor: '#f5f5f5',            //默认nav-bar 背景颜色
      class: '',                             //为nav添加class
      draggable: true,                       //nav tab 可拖动选项
      fixed: false,                          //固定标签头列表
      layout: 'default',                     //有两种模式，'default', 'classic'(所有隐藏tab都在下拉菜单里) 和 'simple'
      maxTabs: 12,                           //最多tab数量。（main和editor不计算在内) 当为1时，整个标签栏隐藏。main和editor分别只能有1个标签。
      maxTabTitleLength: 25,                 //tab标题的最大长度
      showCloseOnHover: false,                //当值为true，仅在鼠标悬浮时显示关闭按钮。false时一直显示
      style: 'nav-tabs',                     //可以为nav-tabs 或 nav-pills
    },
    init: [{
      type: 'main',
      title: '首页',
      url: '/system/index/main.html'
    }]
  });

  $('.nav-item .multitabs').bind('click', function () {
    $('.nav-item').removeClass('active');
    $('.nav-subnav li').removeClass('active');
    $(this).parent('li').addClass('active');
    $(this).parents('.nav-item-has-subnav').addClass('open').first().addClass('active');
  });

});